package com.parkcar;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import HttpUtils.HttpCommunityRequest;
import Modle.AreaBean;
import Modle.SimpleCommunityBean;
import adapter.CommunityAdapter;
import ui.MultipleTextViewGroup;
import util.BaseActivity;
import util.BaseTools;
import util.Constrants;

/**
 * Created by Administrator on 2016/3/19.
 */
public class CommunitySearchActivity extends BaseActivity{
    private EditText search_address;
    private HttpCommunityRequest communityRequest,areaRequest;
    private String keyWord="";
    private long areaId=0;
    private List<String> mReas = new ArrayList<>();
    private MultipleTextViewGroup multiple;
    private CommunityAdapter mAdapter;
    private ListView mListView;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_community_search);
        setTitleGone(View.GONE);
        search_address = (EditText) findViewById(R.id.search_address);
        multiple = (MultipleTextViewGroup) findViewById(R.id.multiple);
        mListView = (ListView) findViewById(R.id.mListView);
        search_address.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if(keyWord.equalsIgnoreCase(search_address.getText().toString().trim())){return;}
                keyWord = search_address.getText().toString().trim();
                getData();
            }
        });
        multiple.setOnMultipleTVItemClickListener(new MultipleTextViewGroup.OnMultipleTVItemClickListener() {
            @Override
            public void onMultipleTVItemClick(View view, int position) {
                if(!BaseTools.isValueEmpty(mReas)){
                    multiple.removeAllViewsInLayout();
                  multiple.setTextViews(mReas,position);
                    areaId = Constrants.mAreas.get(position).getAreaID();
                    getData();
                }
            }
        });
        getArea();
        mAdapter = new CommunityAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle mBundle = new Bundle();
                mBundle.putLong("parkId",mAdapter.getItemId(i));
                showActivity(MonthParkActivity.class,mBundle);
            }
        });
    }

    @Override
    protected void updateUI(boolean isLogin) {

    }
    public void to_cancel(View view){
        finish();
    }
    private void getData(){
        communityRequest = new HttpCommunityRequest(this,0);
        communityRequest.search(keyWord,areaId);
        communityRequest.setmIHttpClinetListener(this);
        communityRequest.doRequest();
    }
    private void getArea(){
        if(BaseTools.isValueEmpty(Constrants.mAreas)){
            areaRequest = new HttpCommunityRequest(this,1);
            areaRequest.setmIHttpClinetListener(this);
            areaRequest.doRequest();
        }else{
            for(AreaBean mBean:Constrants.mAreas){
                mReas.add(mBean.getArea());
            }
            multiple.setTextViews(mReas);
        }
        getData();
    }
    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        if(mBaseResult==null){
            toast("与服务器失去连接");
            return;
        }

        if(communityRequest!=null&&action.equalsIgnoreCase(communityRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0) {
                List<SimpleCommunityBean> mList = mBaseResult.toArray(SimpleCommunityBean.class);
                if(!BaseTools.isValueEmpty(mList)){
                    mAdapter.setData(mList);
                }
            }else{
                toast(mBaseResult.getStatus().getMsg());
            }

        }else if(areaRequest!=null&&action.equalsIgnoreCase(areaRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0){
                Constrants.mAreas = mBaseResult.toArray(AreaBean.class);
                if(!BaseTools.isValueEmpty(Constrants.mAreas)){
                    getArea();
                }
            }
        }
    }
}
